/**
 * 
 */
package com.worksmap.core.service.impl;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.ShopGetRequest;
import com.taobao.api.response.ShopGetResponse;
import com.worksmap.core.constant.RateConstant;
import com.worksmap.core.entity.Shop;
import com.worksmap.core.entity.ShopScore;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.service.ShopService;
import com.worksmap.core.support.TaoBaoClientFactory;
import com.worksmap.core.util.ObjectUtil;

/**
 * @author jack
 *
 */
@Service
public class ShopServiceImpl implements ShopService {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	public Shop getShopInfo(TaoBaoSeller seller) {
		Shop shop = null;
		TaobaoClient client = TaoBaoClientFactory.getClient();
		ShopGetRequest req = new ShopGetRequest();   
		req.setFields(ObjectUtil.getClassFieldStr(Shop.class));
		req.setNick(seller.getTaobao_user_nick());
		try {
			ShopGetResponse response = client.execute(req);
			logger.info("返回的店铺信息:" + response.getBody());
			JSONObject jsonObject = JSONObject.fromObject(response.getBody());
			String json = "";
			if(jsonObject.containsKey("shop_get_response")){
				json = jsonObject.getString("shop_get_response");
				jsonObject = JSONObject.fromObject(json);
			}
			if(jsonObject.containsKey("shop")){
				json = jsonObject.getString("shop");
				JSONObject shopObject = JSONObject.fromObject(json);
				shop = (Shop)JSONObject.toBean(shopObject, Shop.class);
			}
			if(jsonObject.containsKey("shop_score")){
				json = jsonObject.getString("shop_score");
				JSONObject shopScoreObject = JSONObject.fromObject(json);
				ShopScore shopScore = (ShopScore)JSONObject.toBean(shopScoreObject, ShopScore.class);
				shop.setShop_score(shopScore);
			}
			logger.info("shop:" + shop);
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return shop;
	}

}
